//
//  MyMessageViewController.m
//  LoveFreshBee
//
//  Created by brice Mac on 2016/10/20.
//  Copyright © 2016年 王忠迪. All rights reserved.
//
#define navHeight 64
#define subFrame CGRectMake(0, navHeight + 10, kScreenW, kScreenH)

#import "MyMessageViewController.h"
#import "Masonry.h"
#import "HMModel.h"
#import "HMSystemCell.h"


static NSString *reuseId = @"tableCell";


@interface MyMessageViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong)UIView *tableViewUser;

@property (nonatomic,strong)NSArray *modelArray;

@property (nonatomic,assign)CGFloat cellHeight;

@property (nonatomic,strong)NSIndexPath *currentIndex;

@end

@implementation MyMessageViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    //顶部标题栏
    [self createTopView];
    
    //系统消息视图   使用控制器自带的tableView
    
    //用户消息视图
    UIView *tableViewUser = [[UIView alloc]initWithFrame:subFrame];
    self.tableViewUser = tableViewUser;
    [self.view addSubview:tableViewUser];
    
    //设置用户消息视图
    [self createTableUser];
    
    self.tableViewUser.hidden = YES;
    
    
    [HMModel modelWithSuccess:^(NSArray *mArray) {
        
        self.modelArray = mArray;
        
    } error:^{
        
    }];
    
    self.cellHeight = 120;
    
    
    [self.tableView registerClass:[HMSystemCell class] forCellReuseIdentifier:reuseId];
    
}

//调用的时候就给数组赋值
- (void)setModelArray:(NSArray *)modelArray {
    
    _modelArray = modelArray;
    
    [self.tableView reloadData];
}

- (void)setupTableView {
    
    UITableView *tableView = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStyleGrouped];
    self.tableView = tableView;
    [self.view addSubview:tableView];
    tableView.contentInset = UIEdgeInsetsMake(64, 0, 49, 0);
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
}

- (void)createTopView
{
    UISegmentedControl *topSegmentedControl = [[UISegmentedControl alloc]initWithFrame:CGRectMake(0 , 0, kScreenW/2, 30)];
    
//    NSString *sysNews = @"系统消息";
//    NSString *userNews
    [topSegmentedControl setTintColor:[UIColor blackColor]];
    
    [topSegmentedControl insertSegmentWithTitle:@"系统消息" atIndex:0 animated:YES];
    [topSegmentedControl insertSegmentWithTitle:@"用户消息" atIndex:1 animated:YES];
    
    topSegmentedControl.tintColor = [UIColor colorWithRed:253 / 255.0 green:200 / 255.0 blue:8 / 255.0 alpha:1];
    
    
    topSegmentedControl.selectedSegmentIndex = 0;
    
    [topSegmentedControl addTarget:self action:@selector(changeView:) forControlEvents:UIControlEventValueChanged];
    
    [self.navItem setTitleView: topSegmentedControl];
    
}

- (void)changeView:(UISegmentedControl *)seg {
    
    NSInteger index = seg.selectedSegmentIndex;
    
    switch (index) {
        case 0:
            self.tableViewUser.hidden = YES;
            break;
            
        case 1:
            self.tableViewUser.hidden = NO;
            break;
            
        default:
            break;
    }
}


- (void)createTableUser
{
    self.tableViewUser.backgroundColor = [UIColor colorWithRed:239/255.0 green:239/255.0 blue:239/255.0 alpha:1.0];
    
    UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"v2_my_message_empty"]];
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 100)];
    
    label.text = @"~~~并没有消息~~~";
    
    label.textAlignment = NSTextAlignmentCenter;
    
    label.textColor = [UIColor darkGrayColor];
    
    label.font = [UIFont systemFontOfSize:20];
    
    imageView.center = CGPointMake(kScreenW/2, kScreenH/2 - 90);
    
    label.center = CGPointMake(kScreenW/2, CGRectGetMaxY(imageView.frame)+25);
    
    [self.tableViewUser addSubview:imageView];
    
    [self.tableViewUser addSubview:label];
}



/**
    @param tableView dataSource
 */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.modelArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    HMSystemCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseId forIndexPath:indexPath];
    
    cell.model = self.modelArray[indexPath.section];
    
    
    cell.rightBtnCallBack = ^ (UILabel *label){
        // 带动画效果的更新tableView,修改tableViewCell的高度，直接用 reloadData不带动画
//        [self.tableView beginUpdates];
//        [self.tableView endUpdates];
        
        
        [self.tableView reloadData];
    };
    
    return cell;
}





/**
    数据源
 */
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.002;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 20;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    HMSystemCell *cell = [[HMSystemCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseId];
    
    cell.model = self.modelArray[indexPath.section];
    
    NSLog(@"%f",cell.model.rowHeight);
    
    return cell.model.rowHeight;
    
}


@end
